package vn.orderonline.core.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import vn.orderonline.core.dao.common.GenericDaoImpl;
import vn.orderonline.core.dao.entity.District;

@Repository
public class DistrictDaoImpl extends GenericDaoImpl<District, Integer>
		implements DistrictDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> getAllDistrictByCityId(Integer cityId) {
		Criteria criteria = getSession().createCriteria(District.class);
		criteria.setProjection(Projections.projectionList()
				.add(Projections.property("districtId"))
				.add(Projections.property("districtName")));
		criteria.add(Restrictions.eq("cityId", cityId));
		return criteria.list();
	}

	@Override
	public String getDistrictNameByDistrictId(Integer districtId) {
		Criteria criteria = getSession().createCriteria(District.class);
		criteria.setProjection(Projections.property("districtName"));
		criteria.add(Restrictions.eq("districtId", districtId));
		return (String) criteria.uniqueResult();
	}

}
